<?php

namespace App\Http\Service\Goods;

use App\Facade\UserFuncFacade;
use App\Func\Customer\CustomerFunc;
use App\Func\Goods\GoodsConfigFunc;
use App\Func\System\OperateLogFunc;
use App\Func\User\UserFunc;
use App\Http\Service\BasicInterfaceService;
use App\Models\Goods\CartItemsModel;
use App\Models\Goods\GoodsModel;
use Symfony\Component\Uid\UuidV4;

class CartItemsService implements BasicInterfaceService
{

    public function getList(array $prams)
    {

    }

    public function dispose($data): mixed
    {
        return $data;
    }

    public function listSelectFilters($query, $filters)
    {
    }

    public function getSelect()
    {

    }

    public function getSelectHasPage($prams)
    {

    }

    public function getInfo($id)
    {

    }

    public function create($data)
    {
        $info= (new CustomerFunc())->getLoginInfo();
//        $info = (new UserFunc())->getLoginInfo();
        $insertData = [
            'goods_id' => $data['goods_id'],
            'quantity' => $data['quantity']
        ];
        // 获取当前商品信息和库存
        $goodsInfo = (new GoodsModel())->scopeAddComIdWhere()->select([
            'goods_stock'
        ])->where('status',1)
        ->where('enable_status',1)
        ->first();
        if(!$goodsInfo){
            s_fail_return(null,trans('message.created_err',[
                'name' => trans('key.goods_cart')
            ]));
        }
        if($goodsInfo->goods_stock <= $data['quantity']){
            s_fail_return(null,trans('message.goods_stock_insufficient'));
        }


        $insertData['customer_id'] = $info['user_id'];
        $insertData['created_at'] = date('Y-m-d H:i:s');
        $insertData['updated_at'] = date('Y-m-d H:i:s');
//        dd($insertData);
        $cartItemsModel = new CartItemsModel();
        $id = $cartItemsModel->insertGetId($insertData);
        // 添加操作记录
        OperateLogFunc::addOperate(
            'CartItemService',$id,'create'
        );
        s_c_return([
            'cart_items_id' => $id
        ],trans('message.created_ok',[
            'name' => trans('key.goods_cart')
        ]));

    }

    public function fakeDelete($id)
    {
        // 删除购物车
        $cartItemsModel = new CartItemsModel();
        $cartItemsModel->where('cart_items_id',$id)->delete();
        s_c_return([
            'cart_items_id' => $id
        ],trans('message.del_ok',[
            'name' => trans('key.goods_cart')
        ]));
    }

    public function fakeDeleteByIds($ids)
    {

    }

    public function delete($id)
    {

    }

    public function update($data)
    {

    }
}
